package com.xinqi.modules.chat.chat.chat.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.chat.chat.chat.domain.ChatTranRecordEntity;
import com.xinqi.modules.chat.chat.chat.mapper.ChatTranRecordMapper;
import com.xinqi.modules.chat.chat.chat.service.ChatTranRecordService;

import com.xinqi.modules.chat.chat.group.dto.req.ChatTranRecordAddDTO;
import com.xinqi.modules.chat.chat.group.dto.rsp.ChatTranRecordResultDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * 服务层实现
 *
 * @author: yyzhen
 * @date: 2023/03/29
 */
@Service("chatTranRecordService")
@RequiredArgsConstructor
public class ChatTranRecordServiceImpl extends BaseServiceImpl<ChatTranRecordMapper, ChatTranRecordEntity> implements ChatTranRecordService {

    /**
     * 添加转发记录
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean replaceChatTranRecord(ChatTranRecordAddDTO dto) {
        List<ChatTranRecordEntity> elist = new ArrayList<>();
        if (CollectionUtil.isEmpty(dto.getList())) {
            return Boolean.FALSE;
        }
        LocalDateTime now = LocalDateTime.now();
        dto.getList().stream().forEach(item -> {
            ChatTranRecordEntity tranRecord = new ChatTranRecordEntity();
            tranRecord.setTargetId(item.getTargetId());
            tranRecord.setType(item.getType());
            tranRecord.setUserId(dto.getOperationUserId());
            tranRecord.setCreateTime(now);
            elist.add(tranRecord);
        });
        return this.saveBatch(elist);
    }

    /**
     * 查询最近转发（会话单一，查询最多六条）
     *
     * @param userId
     * @return
     */
    @Override
    public List<ChatTranRecordResultDTO> recentChatTranRecordList(Long userId) {
        return this.baseMapper.recentChatTranRecordList(userId);
    }

}
